import 'dart:ffi';

import 'package:dio/dio.dart';
import 'package:miuu_note_app/model/snippet_entity.dart';
import 'dart:async';

import '../config/service_url.dart';
import '../model/entity_factory.dart';
import '../model/msg_entity.dart';

class SnippetPageDao {
  static Future<SnippetPageEntity?> fetch(Map<String, dynamic> map) async {
    try {
      // Map<String, dynamic> map = {"mobile": '1221', "password": '312321'};
      Response response =
          await Dio().post(servicePath['getSnippetPage']!, data: map);
      if (response.statusCode == 200 && response.data['code'] == 200) {
        return EntityFactory.generateOBJ<SnippetPageEntity>(
            response.data['data']);
      } else {
        throw Exception("StatusCode: ${response.statusCode}");
      }
    } catch (e) {
      print(e);
      return null;
    }
  }
}

class SnippetDetailDao {
  static Future<SnippetEntity?> fetch(int snippetId) async {
    try {
      Response response = await Dio()
          .get(servicePath['getSnippetDetail']! + snippetId.toString());
      if (response.statusCode == 200 && response.data['code'] == 200) {
        return EntityFactory.generateOBJ<SnippetEntity>(response.data);
      } else {
        throw Exception("StatusCode: ${response.statusCode}");
      }
    } catch (e) {
      return null;
    }
  }
}

class SnippetSaveOrUpdateDao {
  static Future<MsgEntity?> fetch(Map<String, dynamic> map) async {
    try {
      Response response =
          await Dio().put(servicePath['saveOrUpdateSnippet']!, data: map);
      return EntityFactory.generateOBJ<MsgEntity>(response.data);
    } catch (e) {
      return null;
    }
  }
}
